import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ Title
 * @ Description:
 * @ Auther: 赵彦博
 * @ Date: 2022-12-02
 * @ Time: 15:49
 */
@SuppressWarnings({"all"})

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
@SuppressWarnings({"all"})
public class Solution {

    public ListNode deleteNode(ListNode head, int val) {
        ListNode pre = head;
        if (pre.val == val) {
            return head.next;
        }
        ListNode cur = head.next;
        while (cur != null && cur.val != val) {
            pre = cur;
            cur = cur.next;
        }
        if (cur != null) pre.next = cur.next;
        return head;
    }

    public ListNode getKthFromEnd1(ListNode head, int k) {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        cur = head;
        for (int i = 0; i < count - k; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public ListNode getKthFromEnd2(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }

}
